package com.gitee.kamismile.stone.basic.data.application.service;

import com.gitee.kamismile.stone.basic.data.application.rep.DictionaryDefineRep;
import com.gitee.kamismile.stone.basic.data.application.req.command.dic.SysDictionaryCreate;
import com.gitee.kamismile.stone.basic.data.application.req.command.dic.SysDictionaryDefineCreate;
import com.gitee.kamismile.stone.basic.data.application.req.command.dic.SysDictionaryDefineEdit;
import com.gitee.kamismile.stone.basic.data.application.req.command.dic.SysDictionaryEdit;
import com.gitee.kamismile.stone.basic.data.application.req.query.dic.SysDictionaryDefineQuery;
import com.gitee.kamismile.stone.basic.data.application.req.query.dic.SysDictionaryQuery;
import com.gitee.kamismile.stone.basic.data.domain.sysdic.repository.entity.SysDictionary;
import com.gitee.kamismile.stone.basic.data.domain.sysdic.repository.entity.SysDictionaryDefine;
import com.gitee.kamismile.stone.basic.data.domain.sysdic.service.ISysDictionaryDomainService;
import com.gitee.kamismile.stone.basic.data.infrastructure.enums.CommonErrorEnum;
import com.gitee.kamismile.stone.basic.data.infrastructure.enums.DicErrorEnum;
import com.gitee.kamismile.stone.basic.data.infrastructure.utils.CriteriaQueryBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public class SysDictionaryApplicationService {

    @Autowired
    private ISysDictionaryDomainService iSysDictionaryDomainService;


    /**
     * 查看
     * @param query
     * @return
     */
    public Mono<SysDictionary> findDicByAlias(SysDictionaryQuery query){
        Assert.notNull(query.getnTenantId(), CommonErrorEnum.TenantNotFound.getMessage());
        return iSysDictionaryDomainService.findDicDefineByAlias(query.getAlias(), query.getnTenantId());
    }

    /**
     * 查看字典主配置
     * @param page
     * @param size
     * @param nTenantId
     * @return
     */
    public Mono<Page<SysDictionary>> findDicPageByArgs(int page, int size,
                                                       String sAlias,
                                                       String sName,
                                                       Integer nGroup,
                                                       Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Criteria criteria = CriteriaQueryBuilder.builder()
                .inIgnoreEmpty("nTenantId", new Object[]{0,nTenantId})
                .eqIgnoreEmpty("nGroup", nGroup)
                .likeIgnoreEmpty("sName", sName)
                .likeIgnoreEmpty("sAlias", sAlias)
                .create();
        return iSysDictionaryDomainService.findAllBy(page,size,criteria);
    }


    /**
     * 查看字典子配置
     * @param page
     * @param size
     * @param nTenantId
     * @return
     */
    public Mono<Page<SysDictionaryDefine>> findDicDefinePageByDicId(int page, int size,
                                                                    Integer nSysDictionaryId,
                                                                    Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Criteria criteria = CriteriaQueryBuilder.builder()
                .inIgnoreEmpty("nTenantId", new Object[]{0,nTenantId})
                .eqIgnoreEmpty("nSysDictionaryId", nSysDictionaryId)
                .create();
        return iSysDictionaryDomainService.findDicDefinePageByDicId(page,size,criteria);
    }
    /**
     * 业务页面调用 根据别名 返回所有子配置
     * @return
     */
    public Flux<DictionaryDefineRep> findAllDicDefineByAlias(SysDictionaryDefineQuery query){
        Assert.notNull(query.getnTenantId(), CommonErrorEnum.TenantNotFound.getMessage());
        return iSysDictionaryDomainService.findDicDefineByAlias(query);
    }


    /**
     * 保存主配置
     * @param sysDictionaryCreate
     * @return
     */
    public Mono<SysDictionary> save(SysDictionaryCreate sysDictionaryCreate, Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        SysDictionary sysDictionary=new SysDictionary();
        BeanUtils.copyProperties(sysDictionaryCreate, sysDictionary);
        sysDictionary.setnTenantId(nTenantId);
        return iSysDictionaryDomainService.saveOrUpdate(sysDictionary);
    }
    public Mono<SysDictionary> update(SysDictionaryEdit sysDictionaryEdit, Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.notNull(sysDictionaryEdit.getnSysDictionaryId(), DicErrorEnum.DicNotNull.getMessage());
        return iSysDictionaryDomainService.findAndCheckSysDicById(sysDictionaryEdit.getnSysDictionaryId(), nTenantId)
                .flatMap(v->{
                    SysDictionary sysDictionary=new SysDictionary();
                    BeanUtils.copyProperties(sysDictionaryEdit, sysDictionary);
                    return   iSysDictionaryDomainService.saveOrUpdate(sysDictionary);
                });

    }

    /**
     * 保存子配置
     * @param sysDictionaryDefineCreate
     * @return
     */
    public Mono<SysDictionaryDefine> save(SysDictionaryDefineCreate sysDictionaryDefineCreate, Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        SysDictionaryDefine sysDictionaryDefine=new SysDictionaryDefine();
        BeanUtils.copyProperties(sysDictionaryDefineCreate, sysDictionaryDefine);
        sysDictionaryDefine.setnTenantId(nTenantId);
        return iSysDictionaryDomainService.saveOrUpdate(sysDictionaryDefine);
    }


    public Mono<SysDictionaryDefine> update(SysDictionaryDefineEdit sysDictionaryDefineEdit, Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.notNull(sysDictionaryDefineEdit.getnSysDictionaryDefineId(), DicErrorEnum.DicNotNull.getMessage());
        return iSysDictionaryDomainService.findAndCheckSysDicDefineById(sysDictionaryDefineEdit.getnSysDictionaryDefineId(), nTenantId)
                .flatMap(v->{
                    SysDictionaryDefine sysDictionaryDefine=new SysDictionaryDefine();
                    BeanUtils.copyProperties(sysDictionaryDefineEdit, sysDictionaryDefine);
                    return   iSysDictionaryDomainService.saveOrUpdate(sysDictionaryDefine);
                });

    }

    public Mono<Void> deleteDicById(Integer nSysDictionaryId,Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.notNull(nSysDictionaryId, DicErrorEnum.DicNotNull.getMessage());
        return iSysDictionaryDomainService.findAndCheckSysDicById(nSysDictionaryId, nTenantId)
                .flatMap(v->{
                    return iSysDictionaryDomainService.deleteDicById(nSysDictionaryId, nTenantId);
                });

    }

    public Mono<Void> deleteDicDefineById(Long nSysDictionaryDefineId,Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.notNull(nSysDictionaryDefineId, DicErrorEnum.DicDefNotFound.getMessage());
        return iSysDictionaryDomainService.findAndCheckSysDicDefineById(nSysDictionaryDefineId, nTenantId)
                        .flatMap(v->{
                           return   iSysDictionaryDomainService.deleteDicDefineById(nSysDictionaryDefineId, nTenantId);
                        });
    }
}
